package org.jeecg.lawcase.data.reader;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DataImportManager {
    private final static Logger LG = LoggerFactory.getLogger(DataImportManager.class);
    private static Map<String, DataImportApadter> apadterMap = new ConcurrentHashMap<>();

    /**
     * 获取Excel服务
     * @return
     */
    public static DataImportApadter getExcelervice(){
        return getService("EXCEL");
    }

    /**
     * 获取Text服务
     * @return
     */
    public static DataImportApadter getTextService(){
        return getService("TEXT");
    }

    /**
     * 获取Pdf服务
     * @return
     */
    public static DataImportApadter getPDFService(){
        return getService("PDF");
    }

    /**
     * 获取Html服务
     * @return
     */
    public static DataImportApadter getHtmlService(){
        return getService("HTML");
    }

    /**
     * 获取Image服务
     * @return
     */
    public static DataImportApadter getImageService(){
        return getService("IAMGE");
    }

    /**
     * 获取Csv服务
     * @return
     */
    public static DataImportApadter getCSVService(){
        return getService("CSV");
    }

    /**
     * 获取解析服务
     * @param type
     * @return
     */
    public static DataImportApadter getService(String type){
        DataImportApadter fileServiceApapter = apadterMap.get(type);
        return fileServiceApapter;
    }

    /**
     * 根据文件名称获取服务
     * @param fileName
     * @return
     */
    public static DataImportApadter getServiceByFleName(String fileName){
        String[] fileInfo = fileName.split("\\.");
        if (fileInfo.length <= 1){
            return null;
        }
        String suffixName = fileInfo[fileInfo.length - 1];
        return getService(suffixName.toUpperCase());
    }

    /**
     * 注册服务
     * @param types            服务类型
     * @param taskAdapter       服务实现
     */
    public static void registerService(String types, DataImportApadter taskAdapter){
        for(String type: types.split(",")){
            apadterMap.put(type.toUpperCase(), taskAdapter);
            LG.info("注册数据解析适配器, type={}, apapter={}", type, taskAdapter);
        }
    }

}
